package com.eestimate.services;

import java.util.ArrayList;
import java.util.List;

import com.eestimate.dao.DAO;
import com.eestimate.domain.Chemical;
import com.eestimate.domain.Machine;
import com.eestimate.domain.ToolsAndConsumable;
import com.eestimate.domain.business.BusinessObject;
import com.eestimate.domain.business.common.BusinessObjectTypeEnum;
import com.eestimate.exceptions.ErrorCodeEnum;
import com.eestimate.exceptions.ServiceException;
import com.eestimate.utils.Constants;
import com.eestimate.valuesobjects.ChemicalVo;
import com.eestimate.valuesobjects.MachineVo;
import com.eestimate.valuesobjects.ToolsConsumablesVo;

public class SupplierService {

	private DAO dao;
	
	public ChemicalVo saveChemical(	String cleaningService, String cleaningArea, String application,
									String proposedBrand, String proposedChemical, String dilutionRatio,
									String areaCoverage, String origin, String certification,
									String packing, String totalAreaCoverage, String companyUuid) throws ServiceException {

		try{

			Chemical chemical = (Chemical)dao.emptyBusinessObject(BusinessObjectTypeEnum.CHEMICAL, companyUuid.trim(), Constants.SYSTEM_USER);

			chemical.setCleaningService(cleaningService);
			chemical.setCleaningArea(cleaningArea);
			chemical.setApplication(application);
			chemical.setProposedBrand(proposedBrand);
			chemical.setProposedChemical(proposedChemical);
			chemical.setDilutionRatio(dilutionRatio);
			chemical.setAreaCoverage(areaCoverage);
			chemical.setOrigin(origin);
			chemical.setCertification(certification);
			chemical.setPacking(packing);
			chemical.setTotalAreaCoverage(totalAreaCoverage);
			chemical.setCompanyUuid(companyUuid);
			
			chemical = (Chemical)dao.saveBusinessObject(chemical);
			
			return (ChemicalVo)chemical.createDTO();
		}
		catch(ServiceException exc){
			throw exc;
		}
		catch (Exception e) {
			throw new ServiceException(e.getMessage(), e, ErrorCodeEnum.INTERNAL_SERVER_ERROR);
		}
	}

	public List<ChemicalVo> getChemicalsByCompanyUuid(String companyUuid) throws ServiceException {

		List<ChemicalVo> listOfChemicals = new ArrayList<ChemicalVo>();

		try{
		
			List<BusinessObject> chemicals = dao.getBusinessObjectByName(BusinessObjectTypeEnum.CHEMICAL, companyUuid);
		
			if(chemicals!=null && chemicals.size()>0){
				for (BusinessObject chemical : chemicals) {
					ChemicalVo vo = (ChemicalVo)chemical.createDTO();
					listOfChemicals.add(vo);
				}				
			}
		
			return listOfChemicals;
		
		}
		catch(ServiceException exc){
			throw exc;
		}
		catch (Exception e) {
			throw new ServiceException(e.getMessage(), e, ErrorCodeEnum.INTERNAL_SERVER_ERROR);
		}
	}
	
	public void deleteBusinessObjectByUuid(String uuid) throws ServiceException{
		
		try{
			dao.deleteBusinessObjectByUuid(uuid);}
		catch(ServiceException exc){
			throw exc;
		}
		catch (Exception e) {
			throw new ServiceException(e.getMessage(), e, ErrorCodeEnum.INTERNAL_SERVER_ERROR);
		}
	}
	
	public MachineVo saveMachine(String application, String machineType, String brand, String model, String origin, String fuelType, String runtime, String areaCoverage, String capacity,String companyUuid) throws ServiceException {
		
		try{

			Machine machine = (Machine)dao.emptyBusinessObject(BusinessObjectTypeEnum.MACHINE, companyUuid.trim(), Constants.SYSTEM_USER);
			
			machine.setApplicationPurpose(application);
			machine.setMachineType(machineType);
			machine.setBrand(brand);
			machine.setModel(model);
			machine.setOrigin(origin);
			machine.setFuelType(fuelType);
			machine.setRuntime(runtime);
			machine.setAreaCoverage(areaCoverage);
			machine.setCapacity(capacity);
			machine.setCompanyUuid(companyUuid);
			
			machine = (Machine)dao.saveBusinessObject(machine);
			
			return (MachineVo)machine.createDTO();
		}
		catch(ServiceException exc){
			throw exc;
		}
		catch (Exception e) {
			throw new ServiceException(e.getMessage(), e, ErrorCodeEnum.INTERNAL_SERVER_ERROR);
		}
	}

	public List<MachineVo> getMachinesByCompanyUuid(String companyUuid) throws ServiceException {
		
		List<MachineVo> listOfMachines = new ArrayList<MachineVo>();

		try{
		
			List<BusinessObject> machines = dao.getBusinessObjectByName(BusinessObjectTypeEnum.MACHINE, companyUuid);
		
			if(machines!=null && machines.size()>0){
				for (BusinessObject machine : machines) {
					MachineVo vo = (MachineVo)machine.createDTO();
					listOfMachines.add(vo);
				}				
			}
			
			return listOfMachines;
		
		}
		catch(ServiceException exc){
			throw exc;
		}
		catch (Exception e) {
			throw new ServiceException(e.getMessage(), e, ErrorCodeEnum.INTERNAL_SERVER_ERROR);
		}
	}
	
	public ToolsConsumablesVo saveToolsAndConsumables(String application, String description, String brand, String model, String origin, String packing,String companyUuid) throws ServiceException {
		
		try{
			ToolsAndConsumable toolConsumable = (ToolsAndConsumable)dao.emptyBusinessObject(BusinessObjectTypeEnum.TOOLSANDCONSUMABLES, companyUuid.trim(), Constants.SYSTEM_USER);
			
			toolConsumable.setApplication(application);
			toolConsumable.setDescription(description);
			toolConsumable.setBrand(brand);
			toolConsumable.setModel(model);
			toolConsumable.setOrigin(origin);
			toolConsumable.setPacking(packing);
			toolConsumable.setCompanyUuid(companyUuid);
			
			toolConsumable = (ToolsAndConsumable)dao.saveBusinessObject(toolConsumable);
			
			return (ToolsConsumablesVo)toolConsumable.createDTO();
		}
		catch(ServiceException exc){
			throw exc;
		}
		catch (Exception e) {
			throw new ServiceException(e.getMessage(), e, ErrorCodeEnum.INTERNAL_SERVER_ERROR);
		}
	}

	public List<ToolsConsumablesVo> getToolsAndConsumablesByCompanyUuid(String companyUuid) throws ServiceException {
		
		List<ToolsConsumablesVo> listOfToolsConsumablesVo = new ArrayList<ToolsConsumablesVo>();

		try{
		
			List<BusinessObject> toolsAndConsumables = dao.getBusinessObjectByName(BusinessObjectTypeEnum.TOOLSANDCONSUMABLES, companyUuid);
		
			if(toolsAndConsumables!=null && toolsAndConsumables.size()>0){
				for (BusinessObject tool : toolsAndConsumables) {
					ToolsConsumablesVo vo = (ToolsConsumablesVo)tool.createDTO();
					listOfToolsConsumablesVo.add(vo);
				}				
			}
			
			return listOfToolsConsumablesVo;
		
		}
		catch(ServiceException exc){
			throw exc;
		}
		catch (Exception e) {
			throw new ServiceException(e.getMessage(), e, ErrorCodeEnum.INTERNAL_SERVER_ERROR);
		}
	}
	
	public void setDao(DAO dao) {
		this.dao = dao;
	}

	
}
